package com.softeem.iov.config;

import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//@ConfigurationProperties(prefix = "qiniu.oss")
@Configuration
public class QiniuOssConfig {

    @Value("${qiniu.oss.accessKey}")
    private String accessKey;

    @Value("${qiniu.oss.secretKey}")
    private String secretKey;

    @Value("${qiniu.oss.bucket}")
    private String bucket;

    @Value("${qiniu.oss.domain}")
    private String domain;

    /**
     * 创建认证器
     * @return
     */
    private Auth auth(){
        return Auth.create(accessKey,secretKey);
    }


    /**
     * 创建七牛OSS配置对象
     * @return
     */
    private com.qiniu.storage.Configuration qiniuConfig(){
        return new com.qiniu.storage.Configuration(Region.huadongZheJiang2());
    }

    /**
     * 返回域名
     * @return
     */
    @Bean
    public String domain(){
        return domain;
    }

    /**
     * 返回存储桶名称
     * @return
     */
    @Bean
    public String bucket(){
        return bucket;
    }

    /**
     * 返回token
     * @return
     */
    @Bean
    public String token(){
        return auth().uploadToken(bucket);
    }

    /**
     * 创建上传管理器
     * @return
     */
    @Bean
    public UploadManager uploadManager(){
        return new UploadManager(qiniuConfig());
    }

    /**
     * 创建桶管理器，对文件进行管理操作（删除，更新）
     * @return
     */
    @Bean
    public BucketManager bucketManager(){
        return new BucketManager(auth(),qiniuConfig());
    }
}
